//
//  NSOutlineView+WCExtensions.m
//  sourcebrowser
//
//  Created by William Towe on 1/8/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "NSOutlineView+WCExtensions.h"
#import "WCMacros.h"
#import "NSIndexPath+WCExtensions.h"
#import "NSTreeController+WCExtensions.h"


@implementation NSOutlineView (WCExtensions)
// returns an array of NSIndexPath objects representing the expanded items
- (NSArray *)expandedItems; {
	NSMutableArray *items = [NSMutableArray array];
	NSUInteger row, numberOfRows = [self numberOfRows];
	for (row = 0; row < numberOfRows; row++) {
		id item = [self itemAtRow:row];
		if ([self isItemExpanded:item])
			[items addObject:[item indexPath]]; 
	}
	return [[items copy] autorelease];
}

// expects an array of NSIndexPath objects and expands the items at those index paths using the dataSource (which is always an NSTreeController in our case
- (void)expandItems:(NSArray *)items; {
	for (NSIndexPath *indexPath in items)
		[self expandItem:[[self dataSource] treeNodeAtIndexPath:indexPath]];
}

- (void)editColumn:(NSInteger)column forItem:(id)item; {
	NSInteger row = [self rowForItem:item];
	if (row == -1) {
		row = [self rowForItem:[[self dataSource] treeNodeForRepresentedObject:item]];
		
		if (row == -1)
			return;
	}
	
	[self editColumn:column row:row withEvent:nil select:YES];
}
@end
